package pzl.com.androidloader.loader;

import android.content.Context;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

import pzl.com.androidloader.beans.Bread;

/**
 * @author zl.peng
 * @version [1.0, 2016-10-08]
 *          面包师
 */
public class Baker extends AsyncTaskLoader<List<Bread>> {
    static final String TAG = "Baker";

    BakeryCallback mBakeryCallback;

    /**
     * 面包房回调，用于获得当面面包需求量
     */
    public static interface BakeryCallback {
        int getNeededBreads();
    }

    public Baker(Context context) {
        super(context);
    }

    public Baker(Context context, BakeryCallback bakeryCallback) {
        this(context);
        mBakeryCallback = bakeryCallback;
    }

    /**
     * 面包师生产面包
     *
     * @return
     */
    @Override
    public List<Bread> loadInBackground() {
        List<Bread> breads = new ArrayList();
        /**
         * 获得当前所需的面包
         */
        int needs = mBakeryCallback.getNeededBreads();
        for (int i = 0; i <= needs - 1; i++) {
            breads.add(new Bread());
            //制作面包耗时操作
            Log.d(TAG, "new Bread()");
        }
        return breads;
    }

    @Override
    public void deliverResult(List<Bread> data) {
        super.deliverResult(data);
    }

    @Override
    protected void onStartLoading() {
        super.onStartLoading();
        forceLoad();
    }

    @Override
    protected void onStopLoading() {
        super.onStopLoading();
        cancelLoad();
    }

    @Override
    protected void onReset() {
        super.onReset();
    }
}
